package com.ny.view;
import com.ny.dao.StudentDao;
import com.ny.entity.Order;
import com.ny.entity.Product;
import com.ny.entity.Student;
import com.ny.service.ManagerService;
import com.ny.service.OrderService;
import com.ny.service.ProductService;
import com.ny.service.StudentService;
import com.ny.service.impl.ManagerServiceImpl;
import com.ny.service.impl.OrderServiceImpl;
import com.ny.service.impl.ProductServiceImpl;
import com.ny.service.impl.StudentServiceImpl;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

public class AdminView {
    private ManagerService managerService = new ManagerServiceImpl();
    private  ProductService productService= new ProductServiceImpl();
    private OrderService orderService = new OrderServiceImpl();
    private StudentService studentService = new StudentServiceImpl();
    private Product product;
    private Student  student;

    public AdminView() throws SQLException, ClassNotFoundException {
    }
    public void adminView() throws SQLException, ClassNotFoundException {
        System.out.println("欢迎进入管理员界面");
        System.out.println("请选择相应操作");
        System.out.println("1.管理商品库存");
        System.out.println("2.管理订单");
        System.out.println("3.管理用户");
        System.out.println("0.退出系统");
        Scanner scanner = new Scanner(System.in);
        int select = scanner.nextInt();
        switch (select) {
            case 1:
                System.out.println("管理商品库存");
                System.out.println("请选择要进行的操作");
                System.out.println("1.查询所有商品");
                System.out.println("2.修改商品库存");
                System.out.println("3.添加商品");
                System.out.println("4.删除商品");
                int select1 = scanner.nextInt();
                switch (select1) {
                    case 1:
                        System.out.println("查询所有商品");
                        List<Product> productList = productService.findAllProduct();
                        System.out.println("商品序号" + "\t" + "商品名称" + "\t" + "商品价格" + "\t" + "商品库存" + "\t" + "商品类别" + "\t" + "商品描述");
                        for (Product product : productList) {
                            System.out.print(product.getProduct_id());
                            System.out.print("\t" + "\t");
                            System.out.print(product.getName());
                            System.out.print("\t" + "\t");
                            System.out.print(product.getPrice());
                            System.out.print("\t" + "\t");
                            System.out.print(product.getStock());
                            System.out.print("\t" + "\t");
                            System.out.print(product.getCategory());
                            System.out.print("\t" + "\t");
                            System.out.println(product.getDescription());
                        }
                        break;
                    case 2:
                        System.out.println("修改商品库存");
                        System.out.println("请输入你要修改的商品序号");
                        int productId = scanner.nextInt();
                        System.out.println("请输入你要修改的商品数量");
                        int productNum = scanner.nextInt();
                        if (productId > 0) {
                            boolean updated = productService.updateProductStock(productId, productNum);
                            if (updated) {
                                System.out.println("库存更新成功！");
                            } else {
                                System.out.println("库存更新失败！");
                            }
                        } else {
                            System.out.println("请输入正确的商品id");
                        }
                        break;
                    case 3:
                        System.out.println("添加商品");
                        System.out.println("请输入商品id");
                        int productId2 = scanner.nextInt();
                        System.out.println("请输入商品名称");
                        String name = scanner.next();
                        System.out.println("请输入商品价格");
                        double price = scanner.nextDouble();
                        System.out.println("请输入商品库存");
                        int stock = scanner.nextInt();
                        System.out.println("请输入商品类别");
                        String category = scanner.next();
                        System.out.println("请输入商品描述");
                        String description = scanner.next();
                        Product product = new Product();
                        product.setProduct_id(productId2);
                        product.setName(name);
                        product.setPrice(price);
                        product.setStock(stock);
                        product.setCategory(category);
                        product.setDescription(description);
                        if (productService.addProduct(product) > 0) {
                            System.out.println("添加成功！");
                        } else {
                            System.out.println("添加失败！");
                        }
                        break;
                    case 4:
                        System.out.println("删除商品");
                        System.out.println("请输入你要删除的商品id");
                        int deleteId = scanner.nextInt();
                        if (deleteId > 0) {
                            boolean deleted = productService.deleteProduct(deleteId);
                            if (deleted) {
                                System.out.println("删除成功！");
                            } else {
                                System.out.println("删除失败！");
                            }
                        } else {
                            System.out.println("请输入正确的商品id");
                        }
                        break;
                }
                break;
            case 2:
                System.out.println("管理订单");
                System.out.println("请选择你要进行的操作");
                System.out.println("1.查询所有订单");
                System.out.println("2.修改指定订单");
                System.out.println("3.删除指定订单");
                int select2 = scanner.nextInt();
                switch (select2) {
                    case 1:
                        System.out.println("查询所有订单");
                        List<Order> orderList = orderService.findAllOrder();
                        System.out.println("订单序号" + "\t" + "用户id" + "\t" + "商品id" + "\t" + "数量" + "\t" + "总价");
                        for (Order order : orderList) {
                            System.out.print(order.getOrderId());
                            System.out.print("\t" + "\t");
                            System.out.print(order.getStudentId());
                            System.out.print("\t" + "\t");
                            System.out.print(order.getProductId());
                            System.out.print("\t" + "\t");
                            System.out.print(order.getNumber());
                            System.out.print("\t" + "\t");
                            System.out.println(order.getTotalPrice());
                        }
                        break;
                    case 2:
                        System.out.println("修改指定订单");
                        System.out.println("请输入需要修改的订单id");
                        int orderId = scanner.nextInt();
                        if (orderId > 0) {
                            System.out.println("请输入你要修改的订单数量");
                            int number = scanner.nextInt();
                            boolean updated = orderService.updateOrder(orderId, number);
                            if (updated) {
                                System.out.println("修改成功！");
                            } else {
                                System.out.println("修改失败！");
                            }
                        } else {
                            System.out.println("请输入正确的订单id");
                        }
                        break;
                    case 3:
                        System.out.println("删除订单");
                        System.out.println("请输入你要删除的订单id");
                        int deleteId = scanner.nextInt();
                        if (deleteId > 0) {
                            boolean deleted = orderService.deleteOrder(deleteId);
                            if (deleted) {
                                System.out.println("删除成功！");
                            } else {
                                System.out.println("删除失败！");
                            }
                        } else {
                            System.out.println("请输入正确的订单id");
                        }
                }
                break;
            case 3:
                System.out.println("管理用户");
                System.out.println("请选择你要进行的操作");
                System.out.println("1.查询所有用户");
                System.out.println("2.修改指定用户密码");
                System.out.println("3.添加用户");
                System.out.println("4.删除指定用户");

                int select3 = scanner.nextInt();
                switch (select3) {
                    case 1:
                        System.out.println("查询所有用户");
                        List<Student> studentList = studentService.findAllStudent();
                        System.out.println("用户id" + "\t" + "用户名" + "\t" + "手机号" + "\t" + "密码");
                        for (Student student : studentList) {
                            System.out.print(student.getStudentId());
                            System.out.print("\t" + "\t");
                            System.out.print(student.getStudentName());
                            System.out.print("\t" + "\t");
                            System.out.print(student.getStudentPhone());
                            System.out.print("\t" + "\t");
                            System.out.println(student.getStudentPassword());
                        }
                        break;
                    case 2:
                        System.out.println("修改指定用户密码");
                        System.out.println("请输入需要修改的密码用户id");
                        int studentId = scanner.nextInt();
                        if (studentId > 0) {
                            System.out.println("请输入你要修改的密码");
                            String studentPassword = scanner.next();
                            boolean updated = studentService.updateStudentPassword(studentId, studentPassword);
                            if (updated) {
                                System.out.println("修改成功！");
                            } else {
                                System.out.println("修改失败！");
                            }
                        }
                            break;
                        case 3:
                            System.out.println("添加用户");
                            Student student = new Student();
                            System.out.println("请输入用户名");
                            student.setStudentName(scanner.next());
                            System.out.println("请输入手机号");
                            student.setStudentPhone(scanner.next());
                            System.out.println("请输入密码");
                            student.setStudentPassword(scanner.next());
                            int add = studentService.add(student);
                            if (add > 0) {
                                System.out.println("添加成功！");
                            } else {
                                System.out.println("添加失败！");
                            }
                            break;
                        case 4:
                            System.out.println("删除用户");
                            System.out.println("请输入你要删除的用户id");
                            int deleteId = scanner.nextInt();
                            if (deleteId > 0) {
                                boolean deleted = studentService.deleteStudent(deleteId);
                                if (deleted) {
                                    System.out.println("删除成功！");
                                } else {
                                    System.out.println("删除失败！");
                                }
                            }
                    }
                }
                }
        }